package com.stefanini.pe.bbva.bp.in.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;
import org.springframework.stereotype.Controller;

import com.pe.bbva.utils.Constantes;
import com.stefanini.pe.bbva.bp.in.utils.ConstantesWeb;
import com.stefanini.pe.bbva.bp.in.utils.Util;

@Controller("genericoController")
public class GenericoController {

	private Map<String,Object> parametrosReporte;
	
	
	public GenericoController() {
		parametrosReporte = new HashMap<String, Object>();
	}
	
    public static void mostrarMensaje(String msjResumen) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_INFO, msjResumen, Constantes.VACIO);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarMensaje(String msjResumen, String msjDetalle) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_INFO, msjResumen, msjDetalle);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarWarning(String msjResumen) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_WARN, msjResumen, Constantes.VACIO);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarWarning(String msjResumen, String msjDetalle) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_WARN, msjResumen, msjDetalle);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarError(String msjResumen) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_ERROR, msjResumen, Constantes.VACIO);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarError(String msjResumen, String msjDetalle) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_ERROR, msjResumen, msjDetalle);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarFatal(String msjResumen) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_FATAL, msjResumen, Constantes.VACIO);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void mostrarFatal(String msjResumen, String msjDetalle) {
        FacesMessage msj = new FacesMessage(FacesMessage.SEVERITY_FATAL, msjResumen, msjDetalle);
        FacesContext.getCurrentInstance().addMessage(null, msj);
    }
    
    public static void updateComponente(String[] arrayComponentes){
    	for (String string : arrayComponentes) {
    		if(string.length() > 0){
    			getRequestContext().update(string);
    		}
		}
    }
    
    protected static RequestContext getRequestContext(){
    	return RequestContext.getCurrentInstance();
    }
    
    protected static FacesContext getFacesContext(){
    	return FacesContext.getCurrentInstance();
    }
    
    public static void messageSaveToPopupOrMain(String msgWarnMain, String msgWarnPopup, String msgErrorPopup, 
    		String[] idsMain, String[] idsPopup, String argClosePopup, boolean showMsgMain){
		boolean closePopup = false;
		if(msgWarnPopup.length() == 0 && msgErrorPopup.length() == 0){
			closePopup = true;
			
			boolean updateIdsMain = false;
			for (String string : idsMain) {
	    		if(Util.getStringF(string).trim().length() > 0){
	    			updateIdsMain = true;
	    			break;
	    		}
			}
			
			if(updateIdsMain){
				if(showMsgMain){
					if(msgWarnMain.length()==0){
						mostrarMensaje(ConstantesWeb.ConstanteGenerica.GUARDAR_OK);
					}else{
						mostrarWarning(ConstantesWeb.ConstanteGenerica.GUARDAR_OK+msgWarnMain);
					}
				}
				updateComponente(idsMain);
			}
		}else {
			if(msgWarnPopup.length() > 0){
				mostrarWarning(msgWarnPopup);
			}else if(msgErrorPopup.length() > 0){
				mostrarError(msgErrorPopup);
			}
			updateComponente(idsPopup);
		}
		getRequestContext().addCallbackParam(argClosePopup, closePopup);
    }
    
    protected static HttpSession getHttpSession(){
    	return (HttpSession)(FacesContext.getCurrentInstance().getExternalContext().getSession(true));
    }
    
    protected static Map<String,Object> getSessionMap(){
    	return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    }
    
    protected static Map<String,String> getRequestParameterMap(){
    	return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    }
    
    public Map<String, Object> getParametrosReporte() {
		return parametrosReporte;
	}
    
	protected static InputStream getResourceInputStream(String relPath) {
		return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(relPath);
	}
	
	protected static ServletContext getServletContext(){
		return (ServletContext) getFacesContext().getExternalContext().getContext();
	}
	
	protected HttpServletRequest getRequest(){
		return (HttpServletRequest)(getFacesContext().getExternalContext().getRequest());
	}
}
